package com.yunmang.activity.exception;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.ExceptionQueryBean;
import com.yunmang.entity.NameBean;
import com.yunmang.entity.OrderDetailBean;
import com.yunmang.entity.ParameterEntity.AddExceptionBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/25.
 */
public class ExceptionHandleActivity extends BaseActivity {
    @InjectView(R.id.et_search)
    EditText etSearch;
    @InjectView(R.id.iv_scan)
    ImageView ivScan;
    @InjectView(R.id.btn_search)
    Button btnSearch;
    @InjectView(R.id.tv_check)
    TextView tvCheck;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.et_hdbh_search)
    EditText etHdbhSearch;
    @InjectView(R.id.image_scan)
    ImageView imageScan;
    @InjectView(R.id.ll_search)
    LinearLayout llSearch;
    @InjectView(R.id.search)
    Button search;
    @InjectView(R.id.second_ll_container)
    LinearLayout llContainer;
    @InjectView(R.id.ll_top)
    LinearLayout llTop;
    @InjectView(R.id.order_hdbh)
    TextView orderHdbh;
    @InjectView(R.id.vline)
    View vline;
    @InjectView(R.id.tv_consignee_name)
    TextView tvConsigneeName;
    @InjectView(R.id.tv_consignee_phone)
    TextView tvConsigneePhone;
    @InjectView(R.id.tv_start)
    TextView tvStart;
    @InjectView(R.id.iv_arror)
    ImageView ivArror;
    @InjectView(R.id.tv_end)
    TextView tvEnd;
    @InjectView(R.id.tv_goosname)
    TextView tvGoosname;
    @InjectView(R.id.tv_goods_number)
    TextView tvGoodsNumber;
    @InjectView(R.id.iv_exc_num)
    ImageView ivExcNum;
    @InjectView(R.id.et_exception_number)
    EditText etExceptionNumber;
    @InjectView(R.id.iv_right_exc)
    ImageView ivRightExc;
    @InjectView(R.id.tv_exception_category)
    TextView tvExceptionCategory;
    @InjectView(R.id.rl_exception_category)
    RelativeLayout rlExceptionCategory;
    @InjectView(R.id.iv_right)
    ImageView ivRight;
    @InjectView(R.id.tv_accident)
    TextView tvAccident;
    @InjectView(R.id.rl_exception_accident)
    RelativeLayout rlExceptionAccident;
    @InjectView(R.id.iv_exc_person)
    ImageView ivExcPerson;
    @InjectView(R.id.et_charge_person)
    EditText etChargePerson;
    @InjectView(R.id.iv_right_reason)
    ImageView ivRightReason;
    @InjectView(R.id.et_exception_reason)
    EditText etExceptionReason;
    @InjectView(R.id.btn_add)
    Button btnAdd;
    @InjectView(R.id.et_result)
    EditText etResult;
    @InjectView(R.id.ll_result)
    LinearLayout llResult;
    @InjectView(R.id.rb_pass)
    RadioButton rbPass;
    @InjectView(R.id.rb_nopass)
    RadioButton rbNopass;
    @InjectView(R.id.ll_pass_result)
    LinearLayout llPassResult;
    private ExceptionQueryBean.ModelBean.ItemsBean exceptionBean;
    private Subscriber<OrderDetailBean> subscriber;
    private Subscriber<NameBean> mSubscriber;
    private String status="1";
    private int billid,exceptionId;
    @Override
    public int getLayoutId() {
        return R.layout.activity_exception_report;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        initToolbar();
        llContainer.setVisibility(View.GONE);
        llResult.setVisibility(View.VISIBLE);
        llPassResult.setVisibility(View.VISIBLE);
    }

    private void initToolbar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("异常处理");
        toolbar.setNavigationIcon(R.mipmap.ic_back);

    }

    @Override
    public void initData() {
        exceptionBean = AppConstant.exceptionBean;
        getOrderDetail(exceptionBean.getHdbh().trim());
        orderHdbh.setText(exceptionBean.getHdbh());
        exceptionId=exceptionBean.getId();
    }

    private void getOrderDetail(String search) {
        showWaitingDialog("数据获取中……");
        subscriber = new Subscriber<OrderDetailBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("获取失败");
                hideWaitingDialog();
            }

            @Override
            public void onNext(OrderDetailBean orderDetailBean) {
                hideWaitingDialog();
                if (orderDetailBean.getCode() == 200) {
                    fillUI(orderDetailBean);
                } else {
                    showToast("无数据");
                }
            }
        };
        HttpUtils.getInstance().getOrderDetail(subscriber, search, AppConstant.orgInfo.getId() + "");
    }

    private void fillUI(OrderDetailBean orderDetailBean) {
        OrderDetailBean.ModelBean model = orderDetailBean.getModel();
        tvStart.setText(model.getStartDepartName());
        tvEnd.setText(model.getEndDepartName());
        orderHdbh.setText(model.getBill().getHdbh());
        tvGoosname.setText(model.getGoodsList().get(0).getGoodsName());
        tvGoodsNumber.setText(model.getBill().getQuantity() + "");
        tvConsigneeName.setText(model.getContact().getConsignee());
        tvConsigneePhone.setText(model.getContact().getConsigneePhone());
        etExceptionNumber.setText(exceptionBean.getNumbers() + "");
        billid=model.getBill().getId();
        etExceptionNumber.setHint("");
        etExceptionNumber.setEnabled(false);
        etChargePerson.setHint("");
        etChargePerson.setEnabled(false);
        etExceptionReason.setEnabled(false);
        etExceptionReason.setHint("");
        etExceptionReason.setText(exceptionBean.getApplyReason());
        etChargePerson.setText(exceptionBean.getChargeName());
        tvAccident.setText(exceptionBean.getAccidentTypeName());
        tvExceptionCategory.setText(exceptionBean.getExceptionCategoryName());
    }


    @OnClick(R.id.btn_add)
    public void onClick() {
        //异常处理提交
        String handleRes= CommonUtils.getTextViewValue(etHdbhSearch);
        if(rbPass.isChecked()){
            status="1";
        }else{
            status="2";
        }
        submit(getJsondata(handleRes));
    }
    private String getJsondata(String handleRes){
        Gson gson = new Gson();
        AddExceptionBean addBean = new AddExceptionBean();
        addBean.setUserId(AppConstant.userid);
        addBean.setStatus(status);
        addBean.setHandleResult(handleRes);
        addBean.setClaimAmount("");
        addBean.setHdbh(exceptionBean.getHdbh());
        addBean.setBillId(billid+"");
        addBean.setId(exceptionId);
        return gson.toJson(addBean);
    }
    private void submit(String jsondata) {
        showWaitingDialog("数据上传中……");
        mSubscriber=new Subscriber<NameBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                hideWaitingDialog();
                e.printStackTrace();
                showToast("处理异常");
            }

            @Override
            public void onNext(NameBean nameBean) {
                hideWaitingDialog();
                if(nameBean.getCode()==200){
                    showToast("处理成功");
                    //通知刷新
                    EventBus.getDefault().post("123");
                }else{
                    showToast("处理失败");
                }
            }
        };
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsondata);
        HttpUtils.getInstance().handleException(mSubscriber, body);

    }
}
